<?php

namespace Biz\model;

/**
 *
 * @author lee
 *
 */
class MailTemplate extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop_info';

    const _DATABASE_ = 'shop_info';

    const _TABLE_NAME = 'mail_template';

    const _SUB_TABLE = false;

    private static $emailStyle = <<<STY
		*{
			margin: 0;
			padding: 0;
		}
		.box{
			background: #F1F3F5;
			padding-top: 80px;
		}
		.content{
			margin: 0 auto;
			width: 720px;
			height: 536px;
			background: #FFFFFF;
			box-shadow: 0 1px 2px 0 rgba(0,0,0,0.06);
			padding: 40px 80px;
		}
		.flex_box{
			display: -webkit-box;
		    display: -moz-box;
		    display: -ms-flexbox;
		    display: -webkit-flex;
		    display: flex;
		/*	display: -webkit-box;
		*/}
		.flex_align_items_center{
			-webkit-box-align: center;
		    -ms-flex-align: center;
		    -webkit-align-items: center;
		    align-items: center;
		}
		.title{
			font-family: PingFangSC-Medium;
			font-size: 16px;
			color: #495057;
			letter-spacing: 0;
			line-height: 24px;
		}
		.title img{
			width: 24px;
			height: 26px;
			margin-right: 14px;
		}
		.title_desc{
			font-family: PingFangSC-Medium;
			font-size: 16px;
			color: #495057;
			letter-spacing: 0;
			line-height: 24px;
		}
		p{
			font-family: PingFangSC-Regular;
			font-size: 14px;
			color: #495057;
			letter-spacing: 0;
			line-height: 20px;
		}
		a{
			display: inline-block;
			font-family: PingFangSC-Medium;
			font-size: 12px;
			color: #40C057;
			letter-spacing: 0;
			line-height: 16px;
			margin-top: 16px;
		}
		.hhz{
			float: right;
			font-family: PingFangSC-Regular;
			font-size: 14px;
			color: #495057;
			letter-spacing: 0;
			line-height: 20px;
			margin-top: 80px;
		}
		.hhz_ps{
			font-family: PingFangSC-Regular;
			font-size: 12px;
			color: #CED4DA;
			letter-spacing: 0;
			line-height: 16px;
		}
STY;

    private static $emailTemplate = <<<EVO
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="LECU9nMvm1EqFHPX4mOusC27DRtGritLiVE7SYn-V10" />
    <meta content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,initial-scale=1" name="viewport" /> 
    <meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<title>{title}</title>
	<style>
	{style}
	</style>
</head>
<body>
	<section class="box">
		<section class="content">
			<div class="title flex_box flex_align_items_center">
				<img src="https://img.haohaozhu.com/Op-imageShow/f9843301s01s00000p3i6u1?iv=1" alt="">
				<span class="title_desc">{fromTitle}</span>
			</div>
			{content}
			<div class="hhz">好好住</div>
			<p class="hhz_ps" style="margin-top:140px;">此邮件由好好住发出，请勿回复</p>
			<p class="hhz_ps">版权所有 2014-{now_year} 好好住家宅融智（北京）科技有限公司</p>
		</section>
	</section>
</body>
</html>
EVO;

    public static function getMessage($tag, $params = [])
    {
        $res = self::get(['title', "content"], ['tag' => $tag]);

        if (empty($res))
            return [];

        if (!isset($params['fromTitle'])) {
            $params['fromTitle'] = '好好住商家后台';
        }
        $res['content'] = str_replace('{content}', $res['content'], self::$emailTemplate);

        $params = array_merge($params, [
            'style' => self::$emailStyle,
            'now_year' => date('Y'),
            'send_date' => date('Y-m-d')
        ]);

        $res = array_map(function ($v) use ($params) {
            return self::_formatContent($v, $params);
        }, $res);

        return $res;
    }

    private static function _formatContent($content, $params)
    {
        if (empty($content)) {
            return '';
        }

        if (empty($params)) {
            return $content;
        }

        $keys = array_keys($params);
        $values = array_values($params);


        $keys = array_map(function ($v) {
            return '{' . $v . '}';
        }, $keys);


        $content = str_replace($keys, $values, $content);


        return $content;
    }

}
